package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.navigation.fragment.NavHostFragment;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import cn.kailangtu.commonevents.eventEntity.DataBeanChangedEvent;
import cn.kailangtu.commonevents.eventEntity.FragmentChangedEvent;
import cn.kailangtu.commonevents.eventEntity.LocalSettingChangedEvent;
import cn.kailangtu.commonevents.eventEntity.OnFragmentChangeEvent;
import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.commontools.KeyboardUtils;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.data.ControlTree;
import cn.kailangtu.dataspot.data.ControlTreeNode;
import cn.kailangtu.dataspot.data.HighData;
import cn.kailangtu.dataspot.data.PageIndex;
import cn.kailangtu.dataspot.data.consts.ControlAction;
import cn.kailangtu.dataspot.data.context.SelectContext;
import cn.kailangtu.dataspot.databinding.FragmentCommonSettingBinding;
import cn.kailangtu.dataspot.status;
import cn.kailangtu.dataspot.tasks.BaseTask;

public class CommonSettingFragment extends BaseFragment {

    private FragmentCommonSettingBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentCommonSettingBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().post(new FragmentChangedEvent(R.layout.fragment_common_setting));
        initListener();
    }


    private void initListener()
    {
        //binding.tvCommonSettingTitle32
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OnFragmentChangeEvent event) {
        // Do something
        if(R.layout.fragment_factory_setting == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(CommonSettingFragment.this)
                    .navigate(R.id.action_CommonSettingFragment_to_FactorySettingFragment);
        }
        else if(R.layout.fragment_switch_test == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(CommonSettingFragment.this)
                    .navigate(R.id.action_CommonSettingFragment_to_SwitchTestFragment);
        }
        else if(R.layout.fragment_common_setting == event.getTargetFragmentId())
        {

        }
        else if(R.layout.fragment_exception_log == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(CommonSettingFragment.this)
                    .navigate(R.id.action_CommonSettingFragment_to_ExceptionLogFragment);
        }
        else if(R.layout.fragment_real_picture == event.getTargetFragmentId())
        {

            NavHostFragment.findNavController(CommonSettingFragment.this)
                    .navigate(R.id.action_CommonSettingFragment_to_RealPictureFragment);
        }
    }


    @Override
    protected int processControlsEvent(int eventType) {
        if(eventType == ControlAction.Selection)
        {
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            SelectContext.setSelectedView(textView);
            scrollToPosition();
        }
        else if(eventType == ControlAction.Entry){
            openKeybaord();
        }
        return 0;
    }

    @Override
    protected void buildControlTree() {
        controlTree = ControlTree.getCommonSettingControlTree();
        controlTree.setSelectNode(null);
        resetData();
    }

    @Override
    protected void resetPageIndex() {
        status.localPageIndex = PageIndex.H_COMMAND_SET;
        if (status.localToPlc != 2) {
            BaseTask.toNewPage(status.localPageIndex);
        }
    }

    @Override
    void resetPlatform() {
        if(AppVariable.platform_version == 450 && AppVariable.need_reset_view) {
            commonReset(binding.rlCommonSettingContainer, -20, 0, 0, 20);
        }
    }

    private void scrollToPosition()
    {
        Log.d("SCROLL","ENTRY");
        View view =  SelectContext.getSelectedView();
        if(view.getTag() != null)
        {
            String strTag = view.getTag().toString();
            Log.d("SCROLL","CURRENT TAG:"+strTag);
            if(strTag.startsWith("SCROLL"))
            {
                int line = Integer.parseInt( strTag.substring(6));
                Log.d("SCROLL","CURRENT LINE:"+line);
                binding.svContainer.stopNestedScroll();
                if(line == 1)
                {
                    binding.svContainer.smoothScrollTo(0,0);
                }
                else if(line == 2)
                {
                    binding.svContainer.smoothScrollTo(0,200);
                }
                else if(line == 3)
                {
                    binding.svContainer.smoothScrollTo(0,450);
                }
                else if(line == 4)
                {
                    binding.svContainer.smoothScrollTo(0,700);
                }
                else if(line == 5){
                    binding.svContainer.smoothScrollTo(0,950);
                }
                else if(line == 6)
                {
                    binding.svContainer.smoothScrollTo(0,1400);
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DataBeanChangedEvent event){
        resetData();
    }
    private void resetData()
    {
        processTreeNode(controlTree.getRootNode());
        resetLocalData();
    }

    private void processTreeNode(ControlTreeNode node)
    {

        if(node.getChildren() != null)
        {
            for(int i = 0;i< node.getChildren().size();i++)
            {
                Log.d("SHOOTLEE,,,,","到了索引："+i);
                processTreeNode(node.getChildren().get(i));
            }
        }
        if(node.getProcessType() == 3)
        {
            TextView textView =  this.getActivity().findViewById(node.getTargetControlId());
            textView.setText(APPSPUtils.getInstance().getHostIp());
        }
        else if(node.getAddress() > 0)
        {
            TextView textView =  this.getActivity().findViewById(node.getTargetControlId());
            KeyboardUtils.setNewValue(textView, String.valueOf(HighData.getInstance().getValue(node.getAddress())),node.getSelection(),node.getProcessType());
        }
    }

    private void resetLocalData()
    {
        binding.tvCommonSettingValueB2.setText(APPSPUtils.getInstance().getFangIP());
        binding.tvCommonSettingValueB1.setText(APPSPUtils.getInstance().getFangType());
        binding.tvCommonSettingValueB3.setText(APPSPUtils.getInstance().getFangLevel());
        binding.tvCommonSettingValueB4.setText(APPSPUtils.getInstance().getFangSwitch());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LocalSettingChangedEvent event){
        resetLocalData();
    }

}